<?php

class Core_Frontend_Module_Subject_Industry extends Dcmp_Module_Base {

    protected $name = 'subject_industry';

    public function install() {

    }

    public function uninstall() {

    }

    public function buildConfig($form, $parameters = array()) {
        $datas = App::getModel('foundation/collection_ritu_industry')->load();
        $form->addComboTreeField($this->__('foundation.label.industry'), 'industry_ids', 'module[parameters][industry_ids][]')
                ->setMultiple(TRUE)
                ->bindData($datas->toArray(), 0, array('valueKey' => 'id', 'textKey' => 'name', 'parentKey' => 'parent_code', 'childKey' => 'id'));
        $form->addSelectField($this->__('article.label.condition'), 'sort', 'module[parameters][sort]')
                ->setOption($this->__('article.label.clickcount'), 'click_count')
                ->setOption($this->__('article.label.modified_time'), 'modified_time')
                ->setOption($this->__('article.label.hot'), 'hot')
                ->setOption($this->__('article.label.new'), 'new')
                ->setOption($this->__('article.label.top'), 'top');
        $form->addTextBoxField($this->__('share.label.item_count'), 'item_count', 'module[parameters][item_count]')
                ->addRequiredRule()
                ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.introduction_count'), 'introduction_count', 'module[parameters][introduction_count]')
                ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.introduction_size'), 'introduction_size', 'module[parameters][introduction_size]')
                ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.image_count'), 'image_count', 'module[parameters][image_count]')
                ->setValue(0)
                ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.image_width'), 'image_width', 'module[parameters][image_width]')
                ->setValue(0)
                ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.image_height'), 'image_height', 'module[parameters][image_height]')
                ->setValue(0)
                ->addDigitsRule();
        $form->createTab('module_option', $this->__('design.label.module_parameter'),
                array('field_industry_ids', 'field_sort', 'field_item_count', 'field_introduction_count',
                    'field_introduction_size', 'field_image_count', 'field_image_width', 'field_image_height'));
    }

    public function render() {
        $parameters = $this->getParameters();
        $industries = App::getModel('foundation/collection_ritu_industry')
                        ->filterByCodes($parameters['industry_ids'])
                        ->limit(4,0)
                        ->load();
        $query = App::getModel('subject/collection_content')
                        ->distinct()
                        ->joinIndustry()
                        ->eq('industry_id', ':industryId:s')
                        ->eq('status', ':status:s')
                        ->limit($parameters['item_count'], 0);
        foreach ($industries as $industry) {
            $industry['subjects'] = $query->clear()
                            ->setParameters(
                                    array('industryId' => $industry->getId(), 'status' => 'published'))
                            ->load()
                            ->toArray();
        }
        $tmpl = $this->getTemplate()->assignMulti(array('parameters' => $parameters, 'industries' => $industries));
        return $tmpl->render($this->getTemplatePath());
    }

}

